package site.taingao.async.vertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;

public class MainVerticle extends AbstractVerticle {
    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(MainVerticle.class.getName());
    }

    public void start(Future<Void> startFuture) {
        HttpServer server = vertx.createHttpServer();

        server.requestHandler(request -> {
            HttpServerResponse response = request.response();
            response.putHeader("content-type", "text/plain");
            String uri = request.uri();
            switch (uri) {
                case "/dal":
                    vertx.setTimer(8, l -> response.end("dal ok"));
                    break;
                case "/redis":
                    vertx.setTimer(2, l -> response.end("redis ok"));
                    break;
                case "/image":
                    vertx.setTimer(15, l -> response.end("image ok"));
                    break;
                case "/hello":
                    vertx.setTimer(100, l -> response.end("hello world"));
                    break;
                default:
                    vertx.setTimer(25, l -> response.end("ssss ok"));
            }
        });

        server.listen(8888, http -> {
            if (http.succeeded()) {
                startFuture.complete();
                System.out.println("HTTP server started on port 8888");
            } else {
                startFuture.fail(http.cause());
            }
        });
    }
}
